package org.bit.practice01;

import java.util.Random;

public class Demo01 {
    public static void main(String[] args) throws InterruptedException {
        // 记录开始时间
        long start = System.currentTimeMillis();

        // 1. 创建数组，生成 1 - 100 之间的随机整数
        int total = 1000_0000;
        int[] nums = new int[total];
        Random random = new Random();
        for (int i = 0; i < nums.length; i++) {
            nums[i] = random.nextInt(101);
        }

        // 2. 创建线程，对元素求和
        Sum sum = new Sum();
        // t1 计算奇数下标的和
        Thread t1 = new Thread(() -> {
            for (int i = 1; i < total; i+=2) {
                sum.addOddSum(nums[i]);
            }
        });
        // t2 计算偶数下标的和
        Thread t2 = new Thread(() -> {
            for (int i = 0; i < total; i+=2) {
                sum.addEvenSum(nums[i]);
            }
        });

        // 3. 启动线程
        t1.start();
        t2.start();

        //4. 等待线程
        t1.join();
        t2.join();

        // 记录结束时间
        long end = System.currentTimeMillis();

        System.out.println("程序执行时间为：" + (end - start) + "毫秒");
        System.out.println("计算结果是：" + sum.result());
    }
}

class Sum {
    long oddSum; // 奇数和
    long evenSum; // 偶数和

    public void addOddSum(int num) {
        oddSum += num;
    }

    public void addEvenSum(int num) {
        evenSum += num;
    }

    public long result() {
        System.out.println("偶数和：" + evenSum);
        System.out.println("奇数和：" + oddSum);
        return evenSum + oddSum;
    }
}


    /*public static void main(String[] args) throws InterruptedException {
        // 记录开始时间
        long begin = System.currentTimeMillis();

        // 1. 给定一个很长的数组 (长度 1000w), 通过随机数的方式生成 1-100 之间的整数.
        int total = 1000_0000;
        int[] nums = new int[total];
        Random random = new Random();
        for (int i = 0; i < nums.length; i++) {
            nums[i] = random.nextInt(101);
        }

        // 2. 实现代码, 能够创建两个线程, 对这个数组的所有元素求和.
        // 3. 其中线程1 计算偶数下标元素的和, 线程2 计算奇数下标元素的和.
        SumOperator sumOperator = new SumOperator();
        // 定义线程1
        Thread t1 = new Thread(() -> {
            // 偶数
            for (int i = 0; i < 1000_0000; i += 2) {
                sumOperator.addEvenSum(nums[i]);
            }
        });
        // 定义线程2
        Thread t2 = new Thread(() -> {
            // 偶数
            for (int i = 1; i < 1000_0000; i += 2) {
                sumOperator.addOddSum(nums[i]);
            }
        });

        // 启动线程
        t1.start();
        t2.start();

        // 等待线程
        t1.join();
        t2.join();

        // 记录结束时间
        long end = System.currentTimeMillis();

        // 结果
        System.out.println("计算结果是：" + sumOperator.result());
        System.out.println("程序执行时间为:" + (end - begin) + "ms.");
    }
}

// 累加操作用这个类来完成
class SumOperator {
    long evenSum;// 偶数和
    long oddSum;// 奇数和

    public void addEvenSum(int num) {
        evenSum += num;
    }

    public void addOddSum(int num) {
        oddSum += num;
    }

    public long result() {
        System.out.println("偶数和：" + evenSum);
        System.out.println("奇数和：" + oddSum);
        return evenSum + oddSum;
    }
}*/
